# -*- coding: utf-8 -*-

from blocking_resource import BlockingResource
from response import CommandListResponse
from sgs.sgs_player import SGSPlayerList
from sgs.sgs_game import game
from poker.poker_base import PokerBase

class CommandLineResource(BlockingResource):
    users = SGSPlayerList()
    game.setUserList(users)
    n = 0

    def __init__(self):
        self.poker = PokerBase()
        self.cmd2Handler = {}

    def _render_POST(self, request):
        cmd = request.args['cmd'][0]
        args = request.args['args'][0].split(',')
        return self.handler(cmd, args)

    def handler(self, cmd, args):
        print '<<<cmd=%s, args=%s' % (cmd, args)
        hl = self.cmd2Handler.get(cmd, None)
        if hl:
            rtn_cmd, rtn_args = hl(args)
        else:
            rtn_cmd, rtn_args = game.cmdNotify(cmd, args)
        rtn = [{'cmd': rtn_cmd, 'args': rtn_args}]
        return CommandListResponse(rtn)

